package com.cincc.cincc_pbx.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.cincc.cincc_pbx.R;
import com.cincc.cincc_pbx.model.CallRecordModel;

import java.util.List;

public class CallRecordAdapter extends RecyclerView.Adapter<CallRecordAdapter.MyViewHolder> {

    private Context context;
    private List<CallRecordModel> list;

    public CallRecordAdapter(Context context, List<CallRecordModel> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.call_record_item, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        CallRecordModel item = list.get(position);
        holder.phoneText.setText(item.getPhone());
        holder.beginTimeText.setText(item.getBeginTime());
        if (isConnect(item.getReleaseCause())) {
            holder.durationText.setText(item.getDuration());
            holder.phoneText.setTextColor(ContextCompat.getColor(context, R.color.connect_phone_text_color));
        } else {
            holder.durationText.setText("未接通");
            holder.phoneText.setTextColor(ContextCompat.getColor(context, R.color.un_connect_phone_text_color));
        }
        holder.itemView.setOnClickListener(v -> {
            iOnClickItem.OnClick(item);
        });
    }

    //判断是否接通
    private boolean isConnect(String releaseCause) {
        return releaseCause.equals("0") || releaseCause.equals("1") || releaseCause.equals("2");
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public interface IOnClickItem {
        void OnClick(CallRecordModel item);
    }

    private IOnClickItem iOnClickItem;

    public void setOnClickItem(IOnClickItem iOnClickItem) {
        this.iOnClickItem = iOnClickItem;
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {
        TextView phoneText;
        TextView beginTimeText;
        TextView durationText;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            phoneText = itemView.findViewById(R.id.phone_tv);
            beginTimeText = itemView.findViewById(R.id.begin_time_tv);
            durationText = itemView.findViewById(R.id.duration_tv);
        }
    }
}
